package com.swm.jdo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import com.swm.model.Movie;

/*
public class MovieRepository {
	PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");

	public Collection<Movie> getAll() {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		try {
			List<Movie> movies = new ArrayList<Movie>();
		    Extent<Movie> extent = pm.getExtent(Movie.class, false);
		    for (Movie movie : extent) {
		        movies.add(movie);
		    }
		    extent.closeAll();
			
		    return movies;
		} finally {
			pm.close();
		}
	}

	public void create(Movie movie) {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		try {
		    pm.makePersistent(movie);
		} finally {
			pm.close();
		}
	}

	public void deleteById(Long id) {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		try {
			pm.deletePersistent(pm.getObjectById(Movie.class, id));
		} finally {
			pm.close();
		}
	}
}
*/

public class MovieRepository {
	PersistenceManagerFactory pmfInstance = PMF.get();

	public Collection<Movie> getAll() {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		try {
			List<Movie> movies = new ArrayList<Movie>();
		    Extent<Movie> extent = pm.getExtent(Movie.class, false);
		    for (Movie movie : extent) {
		        movies.add(movie);
		    }
		    extent.closeAll();
			
		    return movies;
		} finally {
			pm.close();
		}
	}

	public void create(Movie movie) {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		try {
		    pm.makePersistent(movie);
		} finally {
			pm.close();
		}
	}

	public void deleteById(Long id) {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		try {
			pm.deletePersistent(pm.getObjectById(Movie.class, id));
		} finally {
			pm.close();
		}
	}
}